//两数相加
class Solution {
public:
    int sum(int num1, int num2) {
        return num1 + num2;
    }
};

//温度转换
//法一
class Solution {
public:
    vector<double> convertTemperature(double celsius) {
        double kelvin = celsius + 273.15;
        double fahrenheit = celsius * 1.80 + 32.00;
        return {kelvin,fahrenheit};
    }
};
//法二
class Solution {
public:
    vector<double> convertTemperature(double celsius) {
        double* ans = (double*)malloc(sizeof(double) * 2);
        ans[0] = celsius + 273.15;
        ans[1] = celsius * 1.80 + 32.00;
        return {ans[0],ans[1]};
    }
};

//最小偶倍数
class Solution {
public:
    int smallestEvenMultiple(int n) {
        if(n % 2)
            return 2 * n;
        else
            return n; 
    }
};

//判断根结点是否等于子节点之和
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool checkTree(TreeNode* root) {
        if(root->val == root->left->val + root->right->val)
            return true;
        else
            return false;
    }
};

//数组异或操作
class Solution {
public:
    int xorOperation(int n, int start) {
        int ans = 0;//为什么选0开始异或：0^x = x
        while(n--){
            ans ^= start;
            start += 2;
        }
        return ans;
    }
};

//好数对
//法一：暴力运算
class Solution {
public:
    int numIdenticalPairs(vector<int>& nums) {
        int count = 0;
        for(int i = 0; i < nums.size(); i++){
            for(int j = i + 1; j < nums.size(); j++){
                if(nums[i] == nums[j])
                    count++;
            }
        }
    return count;
    }
};

//法二：哈希表
class Solution {
public:
    int numIdenticalPairs(vector<int>& nums) {
        int hash[101], ans = 0;
        memset(hash, 0, sizeof(hash));
        for(int i = 0; i < nums.size(); i++){
            ans += hash[nums[i]];//判断在nums[i]前有多少个与之相同的、能结合成好数对的数
            hash[nums[i]]++;
        }
        return ans;
    }
};

//转换成小写字母
//法一
class Solution {
public:
    string toLowerCase(string s) {
        for(int i = 0; s[i] != '\0'; i++){
            if(s[i] >= 'A' && s[i] <= 'Z'){
                s[i] += 32;
            }
        }
        return s;
    }
};

//法二
class Solution {
public:
    string toLowerCase(string s) {
        for(char& ch: s)
            ch = tolower(ch);
        return s;
    }
};
//山脉数组的峰值索引
class Solution {
public:
    int peakIndexInMountainArray(vector<int>& arr) {
        int left = 0, right = arr.size() - 1, mid = 0;
        while(left <= right){
            mid = left + (right - left)/2;
            if(arr[mid] > arr[mid - 1] && arr[mid] > arr[mid + 1]){
                return mid;
            }else if(arr[mid] > arr[mid - 1] && arr[mid] < arr[mid + 1]){
                left = mid;
            }else{
                right = mid;
            }
        }
        return left;
    }
};

//题单：https://leetcode.cn/studyplan/primers-list/